package jalus.utils;

import jalus.pojo.Post;

import java.util.ArrayList;
import java.util.List;

import com.google.gdata.data.MediaContent;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;

public class PojoHandler {

	@SuppressWarnings("unchecked")
	public static Object copyMToP(Object m){
		Object p = null;
		if(m instanceof jalus.model.Post){
			jalus.model.Post mpost = (jalus.model.Post)m;
			p = new Post(mpost.getId(), mpost.getAuthor(), mpost.getTitle(), mpost.getPrivilege(), mpost.getContent().getValue(), mpost.getDate(), mpost.getTags());
		}else if(m instanceof jalus.model.BlogLink){
			jalus.model.BlogLink mb = (jalus.model.BlogLink)m;
			p = new jalus.pojo.BlogLink(mb.getId(), mb.getUrl(), mb.getText(), mb.getDescription());
		}else if(m instanceof jalus.model.Comment){
			jalus.model.Comment mb = (jalus.model.Comment)m;
			jalus.pojo.Comment comment = new jalus.pojo.Comment(mb.getId(), mb.getPostId(), mb.getDate(),mb.getAuthor(), mb.getContent(), mb.getEmail(), mb.getIp());
			comment.setUsername(mb.getUsername());
			if(mb.getHomepage() != null){
				comment.setHomepage(mb.getHomepage().getValue());
			}
			return comment;
		}else if(m instanceof jalus.model.File){
			jalus.model.File mb = (jalus.model.File)m;
			p = new jalus.pojo.File(mb.getId(), mb.getFileName(), mb.getMimeType(), mb.getContent(), mb.getDate());
			((jalus.pojo.File)p).setSize(mb.getContent().getBytes().length);
		}else if(m instanceof jalus.model.FileIndex){
			jalus.model.FileIndex mb = (jalus.model.FileIndex)m;
			p = new jalus.pojo.File(mb.getId(), mb.getFileName(), mb.getMimeType(), null, mb.getDate());
			((jalus.pojo.File)p).setSize(mb.getFileSize());
		}else if(m instanceof jalus.model.Tag){
			jalus.model.Tag mb = (jalus.model.Tag)m;
			p = new jalus.pojo.Tag(mb.getId(), mb.getName(), mb.getCount());
		}else if(m instanceof AlbumEntry){
			AlbumEntry mb = (AlbumEntry)m;
			p = new jalus.pojo.AlbumEntry(mb.getName(), mb.getPhotosUsed(), (mb.getMediaThumbnails().get(0)).getUrl(),	mb.getTitle().getPlainText()); 
		}else if(m instanceof AlbumFeed){
			AlbumFeed mb = (AlbumFeed)m;
			p = new jalus.pojo.AlbumFeed(mb.getName(), mb.getTitle().getPlainText(), copyMToPList(mb.getPhotoEntries()));
		}else if(m instanceof PhotoEntry){
			PhotoEntry mb = (PhotoEntry)m;
			p = new jalus.pojo.PhotoEntry(mb.getMediaThumbnails().get(1).getUrl(), mb.getTitle().getPlainText(), ((MediaContent)mb.getContent()).getUri());
		}
		return p;
	}
	
	@SuppressWarnings("unchecked")
	public static List copyMToPList(List mList){
		List pList = new ArrayList(mList.size());
		for(Object m : mList){
			pList.add(copyMToP(m));
		}
		return pList;
	}
}
